﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using BestTrader.Cms.Core.Options;
using BestTrader.Cms.RmmfSoftService;
using BestTrader.Cms.RmmfSoftServices.Models;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace BestTrader.Cms.RmmfSoftServices
{
    /// <summary>
    /// 子账户成交
    /// </summary>
    public class RmmfOrderService
    {
        protected RmmsoftOption _options;

        public RmmfOrderService(IOptionsSnapshot<RmmsoftOption> options)
        {
            _options = options.Get("RMMSOFT");
            if (_options == null)
            {
                throw new ArgumentNullException(nameof(RmmsoftOption));
            }
        }

        /// <summary>
        /// 子账户成交历史
        /// 1/2  ZH0001/ZH0002
        /// 2019-01-14/24
        /// </summary>
        public List<HistoryDealedOrderOutput> GetHistoryDealed(DateTime fromDate, DateTime toDate, String loginUserName = "QSADMIN")
        {
            var result = new List<HistoryDealedOrderOutput>();
            // [{"DealedDate":"2019/1/24 12:01:10","AccountName":"ZH0001","DealedNumber":"C0QSS00807290","NodeName":"风软对风软测试","Exch_Code":"ICE","ExchCode":"IPE","PfCode":"BRN","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":60.940000,"DealedAsset":60940.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"12:01:10:167","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"522","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190300,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"60.94","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 12:01:00","AccountName":"ZH0001","DealedNumber":"C0QSS00807281","NodeName":"风软对风软测试","Exch_Code":"ICE","ExchCode":"IPE","PfCode":"BRN","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":60.940000,"DealedAsset":60940.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"12:01:00:400","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"522","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190300,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"60.94","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:58:01","AccountName":"ZH0001","DealedNumber":"C0QSS00807062","NodeName":"风软对风软测试","Exch_Code":"SG","ExchCode":"SG","PfCode":"NS","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":20520.000000,"DealedAsset":10260000.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:58:01:247","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"JPY","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"521","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":0.012408,"LMESettleDate":0,"LastTradingDate":20190308,"Carry_Date":0,"DisplayStrikePrice":"0.0","DisplayDealedPrice":"20,520.0","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:57:59","AccountName":"ZH0001","DealedNumber":"C0QSS00807056","NodeName":"风软对风软测试","Exch_Code":"SG","ExchCode":"SG","PfCode":"NS","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":2,"DealedPrice":20520.000000,"DealedAsset":20520000.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:57:59:427","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"JPY","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"521","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":0.012408,"LMESettleDate":0,"LastTradingDate":20190308,"Carry_Date":0,"DisplayStrikePrice":"0.0","DisplayDealedPrice":"20,520.0","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:55:17","AccountName":"ZH0001","DealedNumber":"C0QSS00806781","NodeName":"风软对风软测试","Exch_Code":"SG","ExchCode":"SG","PfCode":"CN","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201902,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":11047.500000,"DealedAsset":11047.500000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:55:17:560","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"517","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190227,"Carry_Date":0,"DisplayStrikePrice":"0.0","DisplayDealedPrice":"11,047.5","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:53:56","AccountName":"ZH0001","DealedNumber":"C0QSS00806693","NodeName":"风软对风软测试","Exch_Code":"NYM","ExchCode":"NYM","PfCode":"PL","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201904,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":798.000000,"DealedAsset":39900.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:53:56:780","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"516","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190426,"Carry_Date":0,"DisplayStrikePrice":"0.0","DisplayDealedPrice":"798.0","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:53:45","AccountName":"ZH0001","DealedNumber":"C0QSS00806682","NodeName":"风软对风软测试","Exch_Code":"NYM","ExchCode":"NYM","PfCode":"PA","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":1311.900000,"DealedAsset":131190.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:53:45:500","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"515","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190327,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"1,311.90","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:49:59","AccountName":"ZH0001","DealedNumber":"C0QSS00806392","NodeName":"风软对风软测试","Exch_Code":"NYM","ExchCode":"NYM","PfCode":"QM","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":52.450000,"DealedAsset":26225.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:49:59:870","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"514","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190219,"Carry_Date":0,"DisplayStrikePrice":"0.000","DisplayDealedPrice":"52.450","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:49:37","AccountName":"ZH0001","DealedNumber":"C0QSS00806369","NodeName":"风软对风软测试","Exch_Code":"NYM","ExchCode":"NYM","PfCode":"QM","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":52.450000,"DealedAsset":26225.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:49:37:527","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"513","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190219,"Carry_Date":0,"DisplayStrikePrice":"0.000","DisplayDealedPrice":"52.450","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:49:24","AccountName":"ZH0001","DealedNumber":"C0QSS00806345","NodeName":"风软对风软测试","Exch_Code":"NYM","ExchCode":"NYM","PfCode":"CL","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":52.430000,"DealedAsset":52430.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:49:24:543","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"512","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190220,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"52.43","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:49:24","AccountName":"ZH0001","DealedNumber":"C0QSS00806344","NodeName":"风软对风软测试","Exch_Code":"NYM","ExchCode":"NYM","PfCode":"CL","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":52.430000,"DealedAsset":52430.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:49:24:333","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"512","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190220,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"52.43","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:46:55","AccountName":"ZH0001","DealedNumber":"C0QSS00806055","NodeName":"风软对风软测试","Exch_Code":"CMX","ExchCode":"CMX","PfCode":"SI","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":15.365000,"DealedAsset":76825.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:46:55:697","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"511","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190327,"Carry_Date":0,"DisplayStrikePrice":"0.000","DisplayDealedPrice":"15.365","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:46:16","AccountName":"ZH0001","DealedNumber":"C0QSS00805927","NodeName":"风软对风软测试","Exch_Code":"CMX","ExchCode":"CMX","PfCode":"QO","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201902,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":1282.000000,"DealedAsset":64100.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:46:16:860","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"510","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190129,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"1,282.00","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:45:19","AccountName":"ZH0001","DealedNumber":"C0QSS00805810","NodeName":"风软对风软测试","Exch_Code":"CMX","ExchCode":"CMX","PfCode":"HG","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":2.659500,"DealedAsset":66487.500000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:45:19:813","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"509","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190327,"Carry_Date":0,"DisplayStrikePrice":"0.0000","DisplayDealedPrice":"2.6595","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:43:01","AccountName":"ZH0001","DealedNumber":"C0QSS00805503","NodeName":"风软对风软测试","Exch_Code":"CMX","ExchCode":"CMX","PfCode":"GC","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201902,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":1282.000000,"DealedAsset":128200.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:43:01:390","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"507","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190226,"Carry_Date":0,"DisplayStrikePrice":"0.0","DisplayDealedPrice":"1,282.0","DisplayLMESettleDate":""},{"DealedDate":"2019/1/24 11:39:32","AccountName":"ZH0001","DealedNumber":"C0QSS00805100","NodeName":"风软对风软测试","Exch_Code":"CME","ExchCode":"CME","PfCode":"NQ","OptionType":100,"OptionTypeName":"期货","StrikePrice":0.000000,"Period":201903,"TradeStyle":"E-Trader","Side":"买","Quantity":1,"DealedPrice":6674.000000,"DealedAsset":133480.000000,"CustFee":0.000000,"FCM_Fee":0.000000,"NetFee":0.000000,"DealedTime":"11:39:32:223","FCM_ID":"","FCM_Code":"","FCM_Name":"","DataFeederName":"ZY_Ord","CustGroup":"","Currency":"USD","userName":"ZH0001","RealName":"KH0001 KH0001","Order_ID":"506","FCM_Exec_ID":"","ForceClose":false,"ByManual":false,"ExchangeRate":1.000000,"LMESettleDate":0,"LastTradingDate":20190315,"Carry_Date":0,"DisplayStrikePrice":"0.00","DisplayDealedPrice":"6,674.00","DisplayLMESettleDate":""}]
            // 创建 HTTP 绑定对象
            var binding = new BasicHttpBinding();
            //设置最大传输接受数量
            binding.MaxReceivedMessageSize = 2147483647;
            // 根据 WebService 的 URL 构建终端点对象
            var endpoint = new EndpointAddress(_options.BaseUrl);
            // 创建调用接口的工厂，注意这里泛型只能传入接口 添加服务引用时生成的 webservice的接口 一般是 (XXXSoap)
            var factory = new ChannelFactory<WebServiceAPISoap>(binding, endpoint);
            // 从工厂获取具体的调用实例
            var callClient = factory.CreateChannel();
            //调用的对应webservice 服务类的函数生成对应的请求类Body (一般是webservice 中对应的方法+RequestBody  如GetInfoListRequestBody)
            GetHistoryDealedOrderReportRequestBody body = new GetHistoryDealedOrderReportRequestBody();
            //以下是为该请求body添加对应的参数（就是调用webService中对应的方法的参数）
            body.fromDate = fromDate;
            body.toDate = toDate;
            body.loginUserName = loginUserName;

            //获取请求对象 （一般是webservice 中对应的方法+tRequest  如GetInfoListRequest）
            var request = new GetHistoryDealedOrderReportRequest(body);
            //发送请求
            var v = callClient.GetHistoryDealedOrderReportAsync(request);
            //异步等待
            v.Wait();
            //获取数据
            result = JsonConvert.DeserializeObject<List<HistoryDealedOrderOutput>>(v.Result.Body.GetHistoryDealedOrderReportResult);

            return result;
        }
    }
}
